package com.meng.message.handler;


import com.alibaba.fastjson.JSONObject;
import com.meng.message.domain.vo.ResponseVO;
import com.meng.message.enums.ErrorEnum;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.Still
 * @version 1.0.0
 * @date 2021-06-14 01:17
 **/
@Component
public class MyFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        ResponseVO<?> result = ResponseVO.result(ErrorEnum.USER_PASSWORD_ERROR);
        response.getWriter().write(JSONObject.toJSONString(result));
    }
}
